<?php
ini_set("include_path", 
    realpath($_SERVER["DOCUMENT_ROOT"] . "/") . PATH_SEPARATOR . ini_get("include_path"));

require_once("classes/Pageley/Util/System.php");
require_once("config/master.php");

$_REQUEST["system.classLocation"] = "classes/";
$_REQUEST["system.resourceLocation"] = "config/";

function pc_pagelet($successView, $parameters = null, $controller = "Include") {
    $pageletTag = new Pageley_Tag_Pagelet();
    $pageletTag->setController($controller);
    $pageletTag->setSuccessView($successView);
    $pageletTag->setContext(pc_variable("pageley.context"));
    if($parameters !== null) {
        foreach($parameters as $name => $value) {
            $pageletTag->setParameter($name, $value);
        }
    }
    $pageletTag->execute();
}

function pc_dispatch($successView = null, $controller = null, $context = null) {
    $dispatchTag = new Pageley_Tag_Dispatch();
    $dispatchTag->setController($controller);
    $dispatchTag->setSuccessView($successView);
    $dispatchTag->setContext($context);
    $dispatchTag->execute();
}

function pc_forward($url) {
    $forwardTag = new Pageley_Tag_Forward();
    $forwardTag->setUrl($url);
    $forwardTag->execute();
}

function pc_link($controller = null, $parameters = null) {
    $linkTag = new Pageley_Tag_Link();
    $linkTag->setController($controller);
    if($parameters !== null) {
        foreach($parameters as $name => $value) {
            $linkTag->setParameter($name, $value);
        }
    }
    return $linkTag->execute();
}

function pc_variable($variableName) {
    $variableTag = new Pageley_Tag_Variable();
    $variableTag->setVariableName($variableName);
    return $variableTag->execute();
}

function pc_config($referenceId) {
    $configTag = new Pageley_Tag_Config();
    $configTag->setReferenceId($referenceId);
    return $configTag->execute();
}

function pc_echo($text, $limitCharacters = 0, $truncate = false, $escape = true) {
    $echoTag = new Pageley_Tag_Echo();
    $echoTag->setText($text);
    $echoTag->setEscape($escape);
    $echoTag->setTruncate($truncate);
    $echoTag->setLimitCharacters($limitCharacters);
    $echoTag->execute();
}

function pc_strip($text) {
    $stripTag = new Pageley_Tag_Strip();
    $stripTag->setText($text);
    return $stripTag->execute();
}

function pc_message($messageKey, $parameters = null) {
    $messageTag = new Pageley_Tag_Message();
    $messageTag->setMessageKey($messageKey);
    if($parameters !== null) {
        foreach($parameters as $name => $value) {
            $messageTag->setParameter($name, $value);
        }
    }
    return $messageTag->execute();
}

function pc_format_number($value, $format) {
	$formatNumberTag = new Pageley_Tag_FormatNumber();
	$formatNumberTag->setValue($value);
	$formatNumberTag->setFormat($format);
	return $formatNumberTag->execute();
}

function pc_format_date($value, $format = "d/m/Y H:i:s") {
    $formatDateTag = new Pageley_Tag_FormatDate();
    $formatDateTag->setValue($value);
    $formatDateTag->setFormat($format);
    return $formatDateTag->execute();
}

function pc_image($file, $width = 0, $height = 0) {
    $imageTag = new Pageley_Tag_Image();
    $imageTag->setFile($file);
    $imageTag->setWidth($width);
    $imageTag->setHeight($height);
    return $imageTag->execute();
}

function pc_set($variableName, $value) {
    $setTag = new Pageley_Tag_Set();
    $setTag->setVariableName($variableName);
    $setTag->setValue($value);
    $setTag->execute();
}

function pc_encode_url($value) {
    $encodeUrlTag = new Pageley_Tag_EncodeUrl();
    $encodeUrlTag->setValue($value);
    return $encodeUrlTag->execute();
}